package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.DateUtility;
import com.fdb.efp.nls.service.facade.LoanRepayPlanBatchService;
import com.fdb.efp.nls.service.facade.*;
import com.fdb.efp.nls.service.vo.*;
import com.fdb.efp.loan.service.vo.LoanRepayPlanVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@Service("loanRepayPlanBatchService")
/**
 * 还款计划
 *
 * @description:TODO
 * @date:2018年12月29日 下午3:21:58
 * @author:cenyj
 */
public class LoanRepayPlanServiceBatchImpl implements LoanRepayPlanBatchService, FrameworkService {
    private static Logger logger = LoggerFactory.getLogger(LoanRepayPlanServiceBatchImpl.class);

    @Autowired
    @Qualifier("accLoanRepayPlanService")
    private LoanRepayPlanService loanRepayPlanService;

    @Autowired
    @Qualifier("gzBankLoanFileTempService")
    private GzBankLoanFileTempService gzBankLoanFileTempService;

    @Autowired
    @Qualifier("repayAcountTempService") //
    private RepayAcountTempService repayAcountTempService;

    @Autowired
    @Qualifier("gzBankRepayPlanFileTempService")
    private GzBankRepayPlanFileTempService gzBankRepayPlanFileTempService;

    @Autowired
    @Qualifier("accoutErroTempService")
    private AccoutErroTempService accoutErroTempService;

    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;

    /**
     *
     * 根據马上同步过来的临时表数据修改還款計劃 还款计划 <br>
     * 0.2:ludayong:2019年3月6日 下午5:10:56:修复还款计划临时信息为空时，出现空指针异常<br>
     * 0.2:ludayong:2019年3月6日 下午5:10:56:增加借据信息累计逾期期数、最高逾期期数的赋值<br>
     * @author cenyj
     * @since 2018年12月27日 下午8:35:23
     * @date 2019年3月6日 下午5:10:56
     * @version 0.2
     */
    public boolean batchUpdateLoanRepayPlanFromMaTxt() throws Exception {
        boolean rs = false;
        try {
            // 先 对账表，看是否能对应上
            logger.info("日终批量修改从马上同步过来的临时表数据到还款明细表：");
            int num = 0;
            // 先校对 对账临时表的记录条数跟同步过来的数据是否一致 如果一致 再批量 把临时表的数据同步到本地
            // 临时表
//			List<GzBankRepayPlanFileTempVO> listFileTempAll = gzBankRepayPlanFileTempService.getGzBankRepayPlanFileTempList();
            //同步信息为空，返回false
//			if(Objects.isNull(listFileTempAll)) {
//				logger.info("日终批量-同步马上的贷款还款计划信息为空！");
//				return rs;
//			}

//			logger.info(
//					"马上同步过来的还款计划临时表数据List<GzBankRepayPlanFileTempVO> listFileTempAll = gzBankRepayPlanFileTempService.getGzBankRepayPlanFileTempList()有："
//							+ listFileTempAll.size() + "条");
            // 分页大小
            Integer pageSize = 200;
            // 分页信息
            GzBankRepayPlanFileTempVO pageInfo = new GzBankRepayPlanFileTempVO();
            // 页数
            int page = 1;
            // pageSize初始化
            if (pageSize > 0) {
                pageInfo.setSize(pageSize);
            } else {
                pageInfo.setSize(200);
            }
            // 判断循环次数
            int size = gzBankRepayPlanFileTempService.queryCount();
            String size_msg = "【还款计划i信息表临时表】数据量：" + size;
            logger.info(size_msg, "message{}");
            int loopCount = (size / pageInfo.getSize()) + 1;

            int total = 0;
            // 对账数据
            BigDecimal principalTotal = BigDecimal.ZERO;
            BigDecimal principalPaidTotal = BigDecimal.ZERO;
            BigDecimal interestTotal = BigDecimal.ZERO;
            BigDecimal interestPaidTotal = BigDecimal.ZERO;
            BigDecimal penaltyDueTotal = BigDecimal.ZERO;
            BigDecimal penaltyPaidTotal = BigDecimal.ZERO;
            for (int i = 0; i < loopCount; i++) {
                String page_msg = "【还款计划i信息表临时表】当前页数：" + pageInfo.getPage() + "，分页大小：" + pageInfo.getSize();
                logger.info(page_msg, "message{}");
                // 分页查询【还款计划信息表临时表】数据
                List<GzBankRepayPlanFileTempVO> tempList = gzBankRepayPlanFileTempService.queryByPage(pageInfo);
                if (Objects.nonNull(tempList) && !tempList.isEmpty()) {
                    // 分页同步还款计划信息
                    syncRepayPlanInfo(tempList);
                    // 对账数据
                    for (GzBankRepayPlanFileTempVO gzBankRepayPlanFileTempVO: tempList) {
                        GzBankRepayPlanFileTempVO tempPlan = new GzBankRepayPlanFileTempVO();
                        tempPlan.setRefNbr(gzBankRepayPlanFileTempVO.getRefNbr());
                        // 拿当前的借据号去临时表查询出来
                        List<GzBankRepayPlanFileTempVO> tempListAll = gzBankRepayPlanFileTempService.getListByRefNbr(tempPlan);
                        if (Objects.nonNull(tempListAll) && !tempListAll.isEmpty()) {
                            total += tempListAll.size();
                            principalTotal = principalTotal.add(tempListAll.stream().map(GzBankRepayPlanFileTempVO::getPrincipal).reduce(BigDecimal.ZERO,
                                    BigDecimal::add));
                            principalPaidTotal = principalPaidTotal.add(tempListAll.stream().map(GzBankRepayPlanFileTempVO::getPrincipalPaid)
                                    .reduce(BigDecimal.ZERO, BigDecimal::add));
                            interestTotal = interestTotal.add(tempListAll.stream().map(GzBankRepayPlanFileTempVO::getInterest).reduce(BigDecimal.ZERO,
                                    BigDecimal::add));
                            interestPaidTotal = interestPaidTotal.add(tempListAll.stream().map(GzBankRepayPlanFileTempVO::getInterestPaid).reduce(BigDecimal.ZERO,
                                    BigDecimal::add));
                            penaltyDueTotal = penaltyDueTotal.add(tempListAll.stream().map(GzBankRepayPlanFileTempVO::getPenaltyDue).reduce(BigDecimal.ZERO,
                                    BigDecimal::add));
                            penaltyPaidTotal = penaltyPaidTotal.add(tempListAll.stream().map(GzBankRepayPlanFileTempVO::getPenaltyPaid).reduce(BigDecimal.ZERO,
                                    BigDecimal::add));
                        }
                    }
                }
                pageInfo.setPage(++page);
            }
            // 对账临时表列表
            List<RepayAcountTempVO> accountList = repayAcountTempService.getRepayAcountTempList();
            logger.info("还款计划开始对账");
            // 对账临时表
//			BigDecimal principalTotal = listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getPrincipal).reduce(BigDecimal.ZERO,
//					BigDecimal::add);
//			BigDecimal principalPaidTotal = listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getPrincipalPaid)
//					.reduce(BigDecimal.ZERO, BigDecimal::add);
//			BigDecimal interestTotal = listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getInterest).reduce(BigDecimal.ZERO,
//					BigDecimal::add);
//			BigDecimal interestPaidTotal = listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getInterestPaid).reduce(BigDecimal.ZERO,
//					BigDecimal::add);
//			BigDecimal penaltyDueTotal = listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getPenaltyDue).reduce(BigDecimal.ZERO,
//					BigDecimal::add);
//			BigDecimal penaltyPaidTotal = listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getPenaltyPaid).reduce(BigDecimal.ZERO,
//					BigDecimal::add);

            // listFileTempAll.stream().map(GzBankRepayPlanFileTempVO::getPenaltyPaid).reduce(BigDecimal.ZERO,
            // BigDecimal::add);

            BigDecimal principalSum = principalTotal == null ? BigDecimal.ZERO : principalTotal;
            BigDecimal principalPaidSum = principalPaidTotal == null ? BigDecimal.ZERO : principalPaidTotal;

            BigDecimal interestSum = interestTotal == null ? BigDecimal.ZERO : interestTotal;
            BigDecimal interestPaidSum = interestPaidTotal == null ? BigDecimal.ZERO : interestPaidTotal;
            BigDecimal penaltyDueSum = penaltyDueTotal == null ? BigDecimal.ZERO : penaltyDueTotal;
            BigDecimal penaltyPaidSum = penaltyPaidTotal == null ? BigDecimal.ZERO : penaltyPaidTotal;

            RepayAcountTempVO accountTempModel = null;//new RepayAcountTempVO();
            int accountCountTemp = 0;// 对账表记录的条数
            int loanTempDatasize = 0;// 实际同步过来的数据总条数
            if (accountList != null && accountList.size() > 0) {
                accountTempModel = accountList.get(0);
            }
            if (accountTempModel != null) {
                accountCountTemp = accountTempModel.getDataCount() == null ? 0 : accountTempModel.getDataCount();
                String tableName = "GzBankRepayPlanFileTemp";
                // BigDecimal aa = BigDecimal.valueOf(accountTempModel.getPrincipalTotal() ==
                // null ? 0 : accountTempModel.getPrincipalTotal());
                if ((accountTempModel.getPrincipalTotal() == null ? new BigDecimal(0) : accountTempModel.getPrincipalTotal())
                        .compareTo(principalSum) != 0) {
                    String erroMsg = "应还本金总额getPrincipalTotal不对";
                    String acountTotal = "" + accountTempModel.getPrincipalTotal();
                    String tempTotal = "" + principalSum;
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
                }
                if ((accountTempModel.getPrincipalPaidTotal() == null ? new BigDecimal(0) : accountTempModel.getPrincipalPaidTotal())
                        .compareTo(principalPaidSum) != 0) {
                    String erroMsg = "已还本金总额getPrincipalPaidTotal不对";
                    String acountTotal = "" + accountTempModel.getPrincipalPaidTotal();
                    String tempTotal = "" + principalPaidSum;
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
                }
                if ((accountTempModel.getInterestTotal() == null ? new BigDecimal(0) : accountTempModel.getInterestTotal())
                        .compareTo(interestSum) != 0) {
                    String erroMsg = "应还利息总额getInterestTotal不对";
                    String acountTotal = "" + accountTempModel.getInterestTotal();
                    String tempTotal = "" + interestSum;
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
                }
                if ((accountTempModel.getInterestPaidTotal() == null ? new BigDecimal(0) : accountTempModel.getInterestPaidTotal())
                        .compareTo(interestPaidSum) != 0) {
                    String erroMsg = "已还利息总额getInterestPaidTotal不对";
                    String acountTotal = "" + accountTempModel.getInterestPaidTotal();
                    String tempTotal = "" + interestPaidSum;
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
                }
                if ((accountTempModel.getPenaltyDueTotal() == null ? new BigDecimal(0) : accountTempModel.getPenaltyDueTotal())
                        .compareTo(penaltyDueSum) != 0) {
                    String erroMsg = "应还罚息总额getPenaltyDueTotal不对";
                    String acountTotal = "" + accountTempModel.getPenaltyDueTotal();
                    String tempTotal = "" + penaltyDueSum;
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
                }
                if ((accountTempModel.getPenaltyPaidTotal() == null ? new BigDecimal(0) : accountTempModel.getPenaltyPaidTotal())
                        .compareTo(penaltyPaidSum) != 0) {
                    String erroMsg = "已还罚息总额getPenaltyPaidTotal不对";
                    String acountTotal = "" + accountTempModel.getPenaltyPaidTotal();
                    String tempTotal = "" + penaltyPaidSum;
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
                }
            }
//			loanTempDatasize = listFileTempAll.size();
            loanTempDatasize = total;

            logger.info("对账表记录的条数loanTempDatasize：" + loanTempDatasize + "实际同步过来的数据总条数TempDatasize" + loanTempDatasize);
            if (loanTempDatasize != accountCountTemp) {
                String erroMsg = "记录条数不对";
                String tableName = "GzBankRepayPlanFileTemp";
                String tempTotal = "" + loanTempDatasize;
                String acountTotal = "" + accountCountTemp;
                accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, "1", "", "", "");
            }
            logger.info("还款计划结束对账");
            // 1从还款计划临时表temp查询所有的借据号 distinct select DISTINCT ref_nbr from
//			List<GzBankRepayPlanFileTempVO> tempRefNbrList = gzBankRepayPlanFileTempService.getRefNbrList();

            logger.info("新增或修改还款计划处理结果rs： " + rs);
            rs = true;
            logger.info("开始处理 提前还清的还款计划数据 ");
            // del
            /// 去借据临时表查询当天-1的并且状态为 （延期）跟提前结清的数据的数据 （有最新的期數）
            // getSequence(String seqId, String orgCode, String prdCode)
            GzBankLoanFileTempVO loanTempBy = new GzBankLoanFileTempVO();
            /*
             * try { // String today =
             * TimeUtil.getTimeStampByPattern(PubConstant.DATE_FORMAT_STAN);
             * SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");// String today
             * = sdfTime.format(new Date());// String yestoday = TimeUtil.addDay(today, -1);
             * // yestoday = TimeUtil.delDateLine(yestoday);
             * loanTempBy.setBizDate(yestoday.replace("-", "")); } catch (Exception e) {
             * logger.error("BizDate日期出错！", e); }
             */
            loanTempBy.setTxnStatus("3");// 提前结清
            //List<GzBankLoanFileTempVO> loanList = gzBankLoanFileTempService.getLoanByDateAndStatus(loanTempBy);
            //logger.info("得到提前还清的还款计划数据 ，记录数：" + loanList.size());
            /// 循环
            /// 得到当前的借据号，删除本地表 条件为 当前的借据号 和 >curr_term期数+1 的数据 (2019002018 修改为 >curr_term期数)

            logger.info("有还款计划数据 ，开始处理");
            logger.info("删除还款计划返回值：rs" + rs);
            rs = true;
        } catch (Exception e) {
            logger.info("还款计划数据发生异常");
            logger.error("还款计划数据发生异常", e);
            e.printStackTrace();
            String tableName = "LoanRepayPlan还款计划表";
            String erroMsg = "";
            String tempTotal = "";
            String acountTotal = "";
            String type = "0";
            String updateDelInster = "";
            String no = "";
            String eMsg = e.getMessage();
            accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster, no, eMsg);

        }
        logger.info("整个还款计划数据方法结束，rs：" + rs);
        return rs;
    }

    /**
     *
     * 获取最高逾期期数 <br>
     * 0.2:ludayong:2019年3月6日 上午8:36:22:TODO <br>
     * @author ludayong
     * @since 2019年3月6日 上午8:36:22
     * @date 2019年3月6日 上午8:36:22
     * @version 0.1 初始为0.1,后续变更则修改
     */
    private int getLoanRepayMaxTimesTotal(List<LoanRepayPlanVO> repayPlanList) {
        int maxTimesTotal = 0;// 最大的违约次数
        int count = 0;// 记数器
        if(Objects.nonNull(repayPlanList)) {
            for (int i = 0; i < repayPlanList.size(); i++) {
                LoanRepayPlanVO loanRepayPlanVO = repayPlanList.get(i);
                if ("1".equals(loanRepayPlanVO.getOverdueFlag())) {
                    count++;
                } else {
                    if (count > maxTimesTotal) {
                        maxTimesTotal = count;
                    }
                    count = 0;// 恢复计数
                }
            }
            if (count > maxTimesTotal) {
                maxTimesTotal = count;
            }
        }
        return maxTimesTotal;
    }

    /**
     *需求编号：【2019D1204】（马上账务文件同步分页处理）
     *问题编号：【2019D1204】（马上账务文件同步分页处理）
     *开发人员：【ludayong】
     *创建日期：【2019/11/27 11:31】
     *功能描述：分页同步还款计划信息
     *修改日期：【2019/11/27 11:31】
     *修改描述：
     */
    public void syncRepayPlanInfo(List<GzBankRepayPlanFileTempVO> tempRefNbrList) {
        try {
            int num = 0;
            // 2循环查出来的借据号
            if (tempRefNbrList != null && tempRefNbrList.size() > 0) {
                logger.info("开始处理从还款计划临时表查询所有的 distinct借据号 ");
                for (int i = 0; i < tempRefNbrList.size(); i++) { // 一条一条的去本地的还款计划表查询，如果没有记录，则用当前的借据号去还款计划的临时表查询对应的所有还款计划信息 新增到本地还款计划表

                    //查找借据，如果借据为null,输出日志，跳过当前借据处理
                    AccLoanVO  acc_loan2 = new AccLoanVO();
                    acc_loan2.setBillNo(tempRefNbrList.get(i).getRefNbr());//
                    AccLoanVO acc_loan = accLoanService.queryByPk(acc_loan2);
                    if(acc_loan==null)
                    {
                        logger.info("没有对应的借据数据信息!");
                        String tableName = "LoanRepayPlan还款计划表";
                        String erroMsg = "";
                        String tempTotal = "";
                        String acountTotal = "";
                        String type = "0";
                        String updateDelInster = "新增";
                        String no = tempRefNbrList.get(i).getRefNbr();
                        String eMsg = "没有对应的借据数据信息!";
                        accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster,
                                no, eMsg);
                        continue;
                    }

                    logger.info("开始循环第" + i + "条更新或新增的还款计划数据");
                    LoanRepayPlanVO lPlan = new LoanRepayPlanVO();
                    lPlan.setRefNbr(tempRefNbrList.get(i).getRefNbr());
                    List<LoanRepayPlanVO> list = loanRepayPlanService.getBankRepayPlanListByRefNbr(lPlan);

                    // 用当前的借据号去还款计划的临时表查询对应的所有本地的还款计划信息
                    GzBankRepayPlanFileTempVO tempPlan = new GzBankRepayPlanFileTempVO();
                    tempPlan.setRefNbr(tempRefNbrList.get(i).getRefNbr());
                    List<GzBankRepayPlanFileTempVO> tempListAll = gzBankRepayPlanFileTempService.getListByRefNbr(tempPlan);// 拿当前的借据号去临时表查询出来
                    //还款计划信息为空
                    if(Objects.isNull(tempListAll)) {
                        continue;
                    }

                    logger.info("用当前的借据号去还款计划的临时表查询对应的所有本地的还款计划信息，得到记录数：" + tempListAll.size());
                    if (list != null && list.size() > 0) { // 有记录则修改
                        // 去临时表拿出来数据
                        // 再根据借据号和期数 去修改本地表的记录
                        List<GzBankRepayPlanFileTempVO> tempListY = tempListAll;// 拿当前的借据号去临时表查询出来
                        for (int a = 0; a < tempListY.size(); a++) {
                            logger.info("开始循环数据，第" + a + "条GzBankRepayPlanFileTempVO的数据，借据号为：" + tempListY.get(a).getRefNbr());
                            try {
                                LoanRepayPlanVO loanRepayPlanBy = new LoanRepayPlanVO();
                                loanRepayPlanBy.setRefNbr(tempListY.get(a).getRefNbr());
                                loanRepayPlanBy.setTerm(tempListY.get(a).getTerm()); //
                                loanRepayPlanBy.setStatus(tempListY.get(a).getStatus()); // LoanRepayPlan loanRepayPlanUpdate = //
                                // LoanRepayPlanVO loanRepayPlanUpdate
                                // =loanRepayPlanService.getLoanRepayPlanByRefNbrAndStatus(loanRepayPlanBy);
                                //loanRepayPlanService.getLoanRepayPlanByRefNbrAndTerm(loanRepayPlanBy);
                                LoanRepayPlanVO loanRepayPlanUpdate = new LoanRepayPlanVO();
//								if (loanRepayPlanUpdate != null) {
                                logger.info("根据借据号和期数 去修改本地表的记录开始");
                                /*
                                 * biz_date begin_date stmt_date grace_date
                                 */
                                SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");//

                                loanRepayPlanUpdate.setBizDate(getBizDate(DateUtility.format8To10(tempListY.get(a).getBizDate())));//
                                loanRepayPlanUpdate.setRefNbr(tempListY.get(a).getRefNbr());
                                loanRepayPlanUpdate.setContrNbr(tempListY.get(a).getRefNbr());// tempListY.get(a).getContrNbr()
                                loanRepayPlanUpdate.setProductCd(tempListY.get(a).getProductCd());
                                loanRepayPlanUpdate.setBeginDate(DateUtility.format8To10(tempListY.get(a).getBeginDate()));
                                loanRepayPlanUpdate.setStmtDate(DateUtility.format8To10(tempListY.get(a).getStmtDate()));
                                loanRepayPlanUpdate.setGraceDate(DateUtility.format8To10(tempListY.get(a).getGraceDate()));
                                loanRepayPlanUpdate.setTerm(tempListY.get(a).getTerm());
                                // status 期数状态 本期借款状态 NUMBER(17,2) Y N:正常 O:逾期 P:结清 F:非应计
                                loanRepayPlanUpdate.setStatus(tempListY.get(a).getStatus()); // 取字典库数据
                                loanRepayPlanUpdate.setPrincipal(tempListY.get(a).getPrincipal());
                                loanRepayPlanUpdate.setPrincipalPaid(tempListY.get(a).getPrincipalPaid());
                                loanRepayPlanUpdate.setPrincipalDue(tempListY.get(a).getPrincipalDue());
                                loanRepayPlanUpdate.setPrincipalDue91(tempListY.get(a).getPrincipalDue91());
                                loanRepayPlanUpdate.setInterestDue(tempListY.get(a).getInterestDue());
                                loanRepayPlanUpdate.setInterest(tempListY.get(a).getInterest());
                                loanRepayPlanUpdate.setInterestPaid(tempListY.get(a).getInterestPaid());
                                loanRepayPlanUpdate.setInterestDue91(tempListY.get(a).getInterestDue91());
                                loanRepayPlanUpdate.setPenaltyDue(tempListY.get(a).getPenaltyDue());
                                loanRepayPlanUpdate.setPenaltyPaid(tempListY.get(a).getPenaltyPaid());
                                loanRepayPlanUpdate.setInitTerm(tempListY.get(a).getInitTerm());
                                // 逾期状态
                                if ("O".equals(tempListY.get(a).getStatus())) {
                                    //逾期标志
                                    loanRepayPlanUpdate.setOverdueFlag("1");
                                }
                                logger.info("根据借据号和期数 去修改本地表的记录结束");

                                LoanRepayPlanVO queryLoanRepayPlanInfo = loanRepayPlanService.getLoanRepayPlanByRefNbrAndTerm(loanRepayPlanBy);
                                if (queryLoanRepayPlanInfo != null) {
                                    num = loanRepayPlanService.updateByPkRefNbrAndTerm(loanRepayPlanUpdate);
                                }else {
                                    num = loanRepayPlanService.insert(loanRepayPlanUpdate);
                                }

                                if (num == -1) {
                                    logger.info("根据借据号和期数 去修改本地表的记录失败");
                                    logger.error("修改数据发生异常!");
                                    String tableName = "LoanRepayPlan还款计划表";
                                    String erroMsg = "";
                                    String tempTotal = "";
                                    String acountTotal = "";
                                    String type = "0";
                                    String updateDelInster = "修改";
                                    String no = tempListY.get(a).getRefNbr();
                                    String eMsg = "";
                                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type,
                                            updateDelInster, no, eMsg);
                                } else {
                                    logger.info("根据借据号和期数 去修改本地表的记录成功");

                                    //删除以下代码，把借据查询放在大循环内 2019-2-25
                                    try {
                                        logger.info("成功后更新借据表相关信息开始");
                                        //AccLoanVO loan = new AccLoanVO();
                                        //loan.setBillNo(tempListY.get(a).getRefNbr());
                                        //AccLoanVO queryAccLoan = accLoanService.queryByPk(loan);
                                        //if (Objects.nonNull(queryAccLoan)) {
                                        ///// 从还款计划关联取相关信息
                                        LoanRepayPlanVO loanRepayPlanInByNo = new LoanRepayPlanVO();
                                        loanRepayPlanInByNo.setRefNbr(acc_loan.getBillNo());

                                        List<LoanRepayPlanVO> repayPlanList = null;
                                        repayPlanList = loanRepayPlanService.queryAllLoanRepayPlan(loanRepayPlanUpdate);
                                        if (repayPlanList != null) {
                                            /*
                                             * // // settl_date 结清日期 赋值？？（结清状态+还款明细时间最大值） 结清时： 还款计划业务日期 List<LocalDate>
                                             * list2 = repayPlanList.stream().filter(o -> o.getStatus().equals("P"))
                                             * .map(LoanRepayPlanVO::getBizDate) .map(o -> LocalDate.parse(o,
                                             * DateTimeFormatter.ISO_DATE)).sorted() .collect(Collectors.toList()); if
                                             * (list2 != null && list2.size() > 0) { String lastTime =
                                             * list2.get(list2.size() - 1).format(DateTimeFormatter.ISO_DATE);
                                             * queryAccLoan.setSettlDate(DateUtility.format8To10(lastTime));
                                             * logger.info("更新借据表结清日期："+lastTime); }
                                             */
                                            BigDecimal interestPaidSums = repayPlanList.stream()
                                                    .map(LoanRepayPlanVO::getInterestPaid).reduce(BigDecimal.ZERO, BigDecimal::add);

                                            BigDecimal actualIntCumuSum = interestPaidSums == null ? BigDecimal.ZERO
                                                    : interestPaidSums;
                                            // actual_int_cumu 实收利息累计 计算赋值（还款计划：interest_paid 已还利息 sum）
                                            acc_loan.setActualIntCumu(actualIntCumuSum);
                                            logger.info("更新借据表实收利息累计：" + actualIntCumuSum);
                                            // over_times_current 当前逾期期数 计算赋值 count 还款计划 逾期状态
                                            if (repayPlanList.size() > 0) {
                                                Long overCount = repayPlanList.stream().filter(m -> m.getStatus().equals("O"))
                                                        .count(); // o逾期
                                                acc_loan.setOverTimesCurrent(new BigDecimal(overCount == null ? 0 : overCount));
                                                // 最高逾期期数:如果出现了逾期，判断当前逾期期数是否 大于 最高逾期期数max_times_total ，如果大于的话：最高逾期期数等于当前逾期期数
                                                //BigDecimal maxTotal = acc_loan.getMaxTimesTotal() == null ? BigDecimal.ZERO
                                                //		: acc_loan.getMaxTimesTotal();
                                                //logger.info(" 最高逾期期数: 判断当前逾期期数是否 大于 最高逾期期数");
                                                //if (new BigDecimal(overCount == null ? 0 : overCount).compareTo(maxTotal) > 0) {
                                                //	acc_loan
                                                //			.setMaxTimesTotal(new BigDecimal(overCount == null ? 0 : overCount));
                                                //	logger.info(" 最高逾期期数: 判断当前逾期期数：" + overCount + " 大于 最高逾期期数：" + maxTotal);
                                                //}
                                                //违约标志
                                                loanRepayPlanInByNo.setOverdueFlag("1");
                                                //通过借据号、违约标志查询还款计划信息
                                                List<LoanRepayPlanVO> overdueFlagPlanList = loanRepayPlanService.queryLoanRepayPlanByLoanNoAndOverdueFlag(loanRepayPlanInByNo);
                                                if(Objects.nonNull(overdueFlagPlanList)) {
                                                    //累计逾期期数
                                                    acc_loan.setOverTimesTotal(new BigDecimal(overdueFlagPlanList.size()));
                                                    logger.info("累计逾期期数为：" + overdueFlagPlanList.size());
                                                }
                                                //最高逾期期数
                                                int maxTimesTotal = this.getLoanRepayMaxTimesTotal(repayPlanList);
                                                BigDecimal maxTotal = acc_loan.getMaxTimesTotal() == null ? BigDecimal.ZERO
                                                        : acc_loan.getMaxTimesTotal();
                                                if (new BigDecimal(maxTimesTotal).compareTo(maxTotal) > 0) {
                                                    acc_loan.setMaxTimesTotal(new BigDecimal(maxTimesTotal));
                                                    logger.info("最高逾期期数为：" + maxTimesTotal);
                                                }
                                            }
                                        }
                                        num = 0;

                                        logger.info(" 更新借据表相关信息开始");
                                        num = accLoanService.updateByPk(acc_loan);// 更新原到期日期、贷款终止日
                                        logger.info(" 更新借据表相关信息结束");
                                        if (num == -1) {
                                            logger.info(" 根据借据号和期数 去修改本地表的记录成功 后 再去更新借据表相关信息失败，第" + a + "条，借据号："
                                                    + tempListY.get(a).getRefNbr());
                                            logger.error("更新数据发生异常");
                                            String tableName = "AccLoan贷款台账表";
                                            String erroMsg = "";
                                            String tempTotal = "";
                                            String acountTotal = "";
                                            String type = "0";
                                            String updateDelInster = "更新";
                                            String no = tempListY.get(a).getRefNbr();
                                            String eMsg = "";
                                            accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type,
                                                    updateDelInster, no, eMsg);

                                        }
                                        //}
                                    } catch (Exception e) {
                                        logger.info(" 根据借据号和期数 去修改本地表的记录失败，第" + a + "条，借据号：" + tempListY.get(a).getRefNbr());
                                        logger.error("更新数据发生异常", e);
                                        e.printStackTrace();
                                        String tableName = "AccLoan贷款台账表";
                                        String erroMsg = "";
                                        String tempTotal = "";
                                        String acountTotal = "";
                                        String type = "0";
                                        String updateDelInster = "更新";
                                        String no = tempListY.get(a).getRefNbr();
                                        String eMsg = e.getMessage();
                                        accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type,
                                                updateDelInster, no, eMsg);
                                    }
                                }
//								}
                            } catch (Exception e) {
                                logger.info(" 根据借据号和期数 去修改本地表的记录失败，第" + a + "条，借据号：" + tempListY.get(a).getRefNbr());
                                logger.error("修改数据发生异常!", e);
                                e.printStackTrace();
                                String tableName = "LoanRepayPlan还款计划表";
                                String erroMsg = "";
                                String tempTotal = "";
                                String acountTotal = "";
                                String type = "0";
                                String updateDelInster = "修改";
                                String no = tempListY.get(a).getRefNbr();
                                String eMsg = e.getMessage();
                                accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster, no,
                                        eMsg);

                            }
                        }
                    } else {
                        // 本地還款計劃表无记录則新增 用当前的借据号去还款计划的临时表查询对应的所有还款计划信息 新增到本地还款计划表
                        // 如果list有记录，就拿当前的借据号去临时表查询出来
                        logger.info("新增到本地还款计划表开始");
                        List<GzBankRepayPlanFileTempVO> tempListN = tempListAll;// 拿当前的借据号去临时表查询出来
                        logger.info("拿当前的借据号去还款计划的临时表查询对应的所有还款计划信息");
                        if (tempListN != null && tempListN.size() > 0) {
                            logger.info("拿当前的借据号去还款计划的临时表查询对应的所有还款计划信息，有记录条数：" + tempListN.size());
                            String maxTermLoanData = "";// 最后还款日期
                            for (int j = 0; j < tempListN.size(); j++) {
                                logger.info("开始循环第" + j + "条数据");
                                // 因爲有可能出现我们审批没有通过，但马上放款的数据，所以先判断本地借据表是否有对应的借据号 有再新增

                                //del 以下代码，把查询借据放在大循环内查询 2019-2-25
                                //AccLoanVO acc_loan = new AccLoanVO();
                                //acc_loan.setBillNo(tempListN.get(j).getRefNbr());//
                                //AccLoanVO accLoanvo = accLoanService.queryByPk(acc_loan);
                                //if (accLoanvo != null) {
                                logger.info("借据表有对应的数据开始新增");
                                try {
                                    LoanRepayPlanVO loanRepayPlanIn = new LoanRepayPlanVO();
                                    loanRepayPlanIn.setBizDate(getBizDate(DateUtility.format8To10(tempListN.get(j).getBizDate())));
                                    loanRepayPlanIn.setContrNbr(tempListN.get(j).getRefNbr());
                                    loanRepayPlanIn.setProductCd(tempListN.get(j).getProductCd());
                                    loanRepayPlanIn.setRefNbr(tempListN.get(j).getRefNbr());
                                    loanRepayPlanIn.setTerm(tempListN.get(j).getTerm());
                                    loanRepayPlanIn.setBeginDate(DateUtility.format8To10(tempListN.get(j).getBeginDate()));
                                    loanRepayPlanIn.setStmtDate(DateUtility.format8To10(tempListN.get(j).getStmtDate()));
                                    loanRepayPlanIn.setGraceDate(DateUtility.format8To10(tempListN.get(j).getGraceDate()));
                                    loanRepayPlanIn.setStatus(tempListN.get(j).getStatus());
                                    loanRepayPlanIn.setPrincipal(tempListN.get(j).getPrincipal());
                                    loanRepayPlanIn.setPrincipalDue(tempListN.get(j).getPrincipalDue());
                                    loanRepayPlanIn.setPrincipalPaid(tempListN.get(j).getPrincipalPaid());
                                    loanRepayPlanIn.setPrincipalDue91(tempListN.get(j).getPrincipalDue91());
                                    loanRepayPlanIn.setInterest(tempListN.get(j).getInterest());
                                    loanRepayPlanIn.setInterestPaid(tempListN.get(j).getInterestPaid());
                                    loanRepayPlanIn.setInterestDue(tempListN.get(j).getInterestDue());
                                    loanRepayPlanIn.setInterestDue91(tempListN.get(j).getInterestDue91());
                                    loanRepayPlanIn.setPenaltyDue(tempListN.get(j).getPenaltyDue());
                                    loanRepayPlanIn.setPenaltyPaid(tempListN.get(j).getPenaltyPaid());
                                    // 还款总期数不应大于12期 -init_term init_term
                                    /*
                                     * if(tempListN.get(j).getInitTerm() == accLoanvo.getLoanTerm())//init_term
                                     * loan_term 放款总期数（贷款期限）（在本地表缺少需加上） {
                                     *
                                     * }
                                     */
                                    // 逾期状态
                                    if ("O".equals(tempListN.get(j).getStatus())) {
                                        //逾期标志
                                        loanRepayPlanIn.setOverdueFlag("1");
                                    } else {
                                        //逾期标志
                                        loanRepayPlanIn.setOverdueFlag("0");
                                    }
                                    loanRepayPlanIn.setInitTerm(tempListN.get(j).getInitTerm());
                                    logger.info("借据表有对应的数据结束新增");
                                    num = loanRepayPlanService.insert(loanRepayPlanIn);
                                    logger.info("借据表有对应的数据结束新增，返回值num：" + num);
                                    if (num == -1) {
                                        logger.info("借据表有对应的数据新增失败");
                                        logger.error("新增数据发生异常");
                                        String tableName = "LoanRepayPlan还款计划表";
                                        String erroMsg = "";
                                        String tempTotal = "";
                                        String acountTotal = "";
                                        String type = "0";
                                        String updateDelInster = "新增";
                                        String no = tempListN.get(j).getRefNbr();
                                        String eMsg = "";
                                        accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type,
                                                updateDelInster, no, eMsg);

                                    }
                                } catch (Exception e) {
                                    logger.info("借据表有对应的数据开始新增后出现异常，第" + j + "条", "借据号" + tempListN.get(j).getRefNbr());

                                    logger.error("新增数据发生异常", e);
                                    e.printStackTrace();
                                    String tableName = "LoanRepayPlan还款计划表";
                                    String erroMsg = "";
                                    String tempTotal = "";
                                    String acountTotal = "";
                                    String type = "0";
                                    String updateDelInster = "新增";
                                    String no = tempListN.get(j).getRefNbr();
                                    String eMsg = e.getMessage();
                                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type,
                                            updateDelInster, no, eMsg);

                                }

                                //del 以下代码，放在大循环内查询 2019-2-25
//								} else {
//									logger.info("没有对应的借据数据信息!");
//									String tableName = "LoanRepayPlan还款计划表";
//									String erroMsg = "";
//									String tempTotal = "";
//									String acountTotal = "";
//									String type = "0";
//									String updateDelInster = "新增";
//									String no = tempListN.get(j).getRefNbr();
//									String eMsg = "没有对应的借据数据信息!";
//									accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster,
//											no, eMsg);
//
//								}
                                try
                                {
                                    // 拿临时表还款计划的还款期数term 等于本地借据的 放款总期数loan_term时，取还款计划的日期
                                    BigDecimal loanTerm = acc_loan.getLoanTerm() == null ? new BigDecimal(0) :  acc_loan.getLoanTerm();

                                    if ((tempListN.get(j).getTerm() == null ? new BigDecimal(0) : new BigDecimal(tempListN.get(j).getTerm()))
                                            .compareTo( loanTerm ) == 0) {
                                        maxTermLoanData = DateUtility.format8To10(tempListN.get(j).getStmtDate());
                                    }
                                } catch (Exception e) {
                                    logger.info("拿临时表还款计划的还款期数term 等于本地借据的 放款总期数loan_term时，取还款计划的日期出现异常，第" + i + "条", "借据号" + tempListN.get(i).getRefNbr());
                                    logger.error("更新数据发生异常", e);
                                    e.printStackTrace();
                                    String tableName = "AccLoan贷款台账表";
                                    String erroMsg = "";
                                    String tempTotal = "";
                                    String acountTotal = "";
                                    String type = "0";
                                    String updateDelInster = "更新";
                                    String no = tempRefNbrList.get(i).getRefNbr();
                                    String eMsg = e.getMessage();
                                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster, no,
                                            eMsg);
                                }
                            }
                            logger.info("借据表有对应的数据新增成功");
                            // 该借据的还款计划新增成功之后修改 借据相关信息
                            logger.info("该借据的还款计划新增成功之后修改 借据相关信息");
                            try {
                                //AccLoanVO loan = new AccLoanVO();
                                //loan.setBillNo(tempRefNbrList.get(i).getRefNbr());
                                //AccLoanVO queryAccLoan = accLoanService.queryByPk(loan);

                                LoanRepayPlanVO loanRepayPlanInByNo = new LoanRepayPlanVO();
                                loanRepayPlanInByNo.setRefNbr(acc_loan.getBillNo());

                                List<LoanRepayPlanVO> repayPlanList = null;
                                repayPlanList = loanRepayPlanService.queryAllLoanRepayPlan(loanRepayPlanInByNo);
                                if (repayPlanList != null) {
                                    BigDecimal interestPaidSums = repayPlanList.stream().map(LoanRepayPlanVO::getInterestPaid)
                                            .reduce(BigDecimal.ZERO, BigDecimal::add);

                                    BigDecimal actualIntCumuSum = interestPaidSums == null ? BigDecimal.ZERO : interestPaidSums;
                                    // actual_int_cumu 实收利息累计 计算赋值（还款计划：interest_paid 已还利息 sum）
                                    acc_loan.setActualIntCumu(actualIntCumuSum);
                                    logger.info("更新借据表实收利息累计：" + actualIntCumuSum);
                                    // over_times_current 当前逾期期数 计算赋值 count 还款计划 逾期状态
                                    if (repayPlanList.size() > 0) {
                                        Long overCount = repayPlanList.stream().filter(m -> m.getStatus().equals("O")).count(); // o逾期
                                        acc_loan.setOverTimesCurrent(new BigDecimal(overCount == null ? 0 : overCount));
                                        //违约标志
                                        loanRepayPlanInByNo.setOverdueFlag("1");
                                        //通过借据号、违约标志查询还款计划信息
                                        List<LoanRepayPlanVO> overdueFlagPlanList = loanRepayPlanService.queryLoanRepayPlanByLoanNoAndOverdueFlag(loanRepayPlanInByNo);
                                        if(Objects.nonNull(overdueFlagPlanList)) {
                                            //累计逾期期数
                                            acc_loan.setOverTimesTotal(new BigDecimal(overdueFlagPlanList.size()));
                                            logger.info("累计逾期期数为：" + overdueFlagPlanList.size());
                                        }
                                        //最高逾期期数
                                        int maxTimesTotal = this.getLoanRepayMaxTimesTotal(repayPlanList);
                                        BigDecimal maxTotal = acc_loan.getMaxTimesTotal() == null ? BigDecimal.ZERO
                                                : acc_loan.getMaxTimesTotal();
                                        if (new BigDecimal(maxTimesTotal).compareTo(maxTotal) > 0) {
                                            acc_loan.setMaxTimesTotal(new BigDecimal(maxTimesTotal));
                                            logger.info("最高逾期期数为：" + maxTimesTotal);
                                        }
                                    }
                                }

                                //acc_loan.setOrigExpiDate(maxTermLoanData);// 原到期日期
                                //acc_loan.setLoanEndDate(maxTermLoanData);// 贷款终止日
                                logger.info("还款计划新增成功后，修改 借据对应的信息结束");
                                num = accLoanService.updateByPk(acc_loan);// 更新原到期日期、贷款终止日
                                logger.info("还款计划新增成功后，修改 借据对应的信息，返回值num：" + num);
                                if (num == -1) {
                                    logger.info("还款计划新增成功后，修改 借据对应的信息失败，第" + i + "条", "借据号" + tempListN.get(i).getRefNbr());

                                    logger.error("更新数据发生异常");
                                    String tableName = "AccLoan贷款台账表";
                                    String erroMsg = "";
                                    String tempTotal = "";
                                    String acountTotal = "";
                                    String type = "0";
                                    String updateDelInster = "更新";
                                    String no = tempRefNbrList.get(i).getRefNbr();
                                    String eMsg = "";
                                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster,
                                            no, eMsg);

                                }
                            } catch (Exception e) {
                                logger.info("借据表有对应的数据新增后出现异常，第" + i + "条", "借据号" + tempListN.get(i).getRefNbr());
                                logger.error("更新数据发生异常", e);
                                e.printStackTrace();
                                String tableName = "AccLoan贷款台账表";
                                String erroMsg = "";
                                String tempTotal = "";
                                String acountTotal = "";
                                String type = "0";
                                String updateDelInster = "更新";
                                String no = tempRefNbrList.get(i).getRefNbr();
                                String eMsg = e.getMessage();
                                accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster, no,
                                        eMsg);
                            }
                        }

                    }
                    logger.info("结束循环第" + i + "条更新或新增的还款计划数据");
                }
                logger.info("结束处理从还款计划临时表查询所有的 distinct借据号 ");
            }
        } catch (Exception e) {
            logger.error("同步马上还款计划信息数据发生异常!", e);
        }
    }

    /**
     *需求编号：【2019D1204】（马上账务文件同步分页处理）
     *问题编号：【2019D1204】（马上账务文件同步分页处理）
     *开发人员：【ludayong】
     *创建日期：【2019/11/27 11:31】
     *功能描述：根据借据状态分页查询借据信息
     *修改日期：【2019/11/27 11:31】
     *修改描述：
     */
    public void queryLoanInfoStatusByPage(GzBankLoanFileTempVO loanTempBy) {
        try {
            // 分页大小
            Integer pageSize = 1000;
            // 分页信息
            GzBankLoanFileTempVO pageInfo = new GzBankLoanFileTempVO();
            // 页数
            int page = 1;
            // pageSize初始化
            if (pageSize > 0) {
                pageInfo.setSize(pageSize);
            } else {
                pageInfo.setSize(1000);
            }
            // 判断循环次数
            int size = gzBankLoanFileTempService.queryCountByDateAndStatus();
            String size_msg = "根据借据状态分页查询【借据信息表临时表】数据量：" + size;
            logger.info(size_msg, "message{}");
            int loopCount = (size / pageInfo.getSize()) + 1;
            for (int i = 0; i < loopCount; i++) {
                String page_msg = "根据借据状态分页查询【借据信息表临时表】当前页数：" + pageInfo.getPage() + "，分页大小：" + pageInfo.getSize();
                logger.info(page_msg, "message{}");
                // 分页查询【借据信息表临时表】数据
                List<GzBankLoanFileTempVO> tempList = gzBankLoanFileTempService.queryLoanByDateAndStatusByPage(pageInfo);
                if (Objects.nonNull(tempList) && !tempList.isEmpty()) {
                    // 同步借据信息
                    syncLoanInfo(tempList);
                }
                pageInfo.setPage(++page);
            }
        } catch (Exception e) {
            logger.error("同步马上借据信息出错!", e);
            e.printStackTrace();
            String tableName = "AccLoan借据表";
            String erroMsg = "";
            String tempTotal = "";
            String acountTotal = "";
            String type = "0";
            String updateDelInster = "更新";
            String no = "";
            String eMsg = e.getMessage();
            accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster, no, eMsg);
        }
    }

    /**
     *需求编号：【2019D1204】（马上账务文件同步分页处理）
     *问题编号：【2019D1204】（马上账务文件同步分页处理）
     *开发人员：【ludayong】
     *创建日期：【2019/11/27 11:31】
     *功能描述：根据借据状态分页同步借据信息
     *修改日期：【2019/11/27 11:31】
     *修改描述：
     */
    public boolean syncLoanInfo(List<GzBankLoanFileTempVO> loanList) {
        boolean result = false;
        int num = 0;
        if (loanList != null && loanList.size() > 0) {
            logger.info("有还款计划数据 ，开始处理");
            for (int i = 0; i < loanList.size(); i++) {
                logger.info("循环处理提前结清的还款计划数据，第" + i + "条，借据号：" + loanList.get(i).getRefNbr());
                try {
                    LoanRepayPlanVO loanRepayPlan = new LoanRepayPlanVO();
                    loanRepayPlan.setRefNbr(loanList.get(i).getRefNbr());// 借据号

                    // ***************************20190123***********//
                    loanRepayPlan.setTerm(loanList.get(i).getCurrTerm());// + 1 期数 2019002018 修改为 不加1
                    /*
                     * AccLoanVO inAccLoan = new AccLoanVO();
                     * inAccLoan.setBillNo(loanList.get(i).getRefNbr());// 借据号 AccLoanVO
                     * queryAccLoan = accLoanService.queryByPk(inAccLoan); if (queryAccLoan != null)
                     * { loanRepayPlan.setTerm(queryAccLoan.getCurrTerm().intValue() + 1);// 期数 }
                     */
                    // ***************************20190123***********//
                    logger.info("根据当前的借据号，删除本地表 条件为 当前的借据号 和 >curr_term期数，第" + i + "条，借据号：" + loanList.get(i).getRefNbr() + "期数："
                            + loanList.get(i).getCurrTerm());
                    logger.info("删除本地表 条件为 当前的借据号 和 >curr_term期数还款计划数据 ，开始");
                    num = loanRepayPlanService.deleteByRefNbrAndTerm(loanRepayPlan);
                    logger.info("删除本地表 条件为 当前的借据号 和 >curr_term期数还款计划数据 ，结束，返回值num：" + num);
                    if (num == -1) {
                        logger.info("删除本地表 条件为 当前的借据号 和 >curr_term期数还款计划数据 ，失败");
                    }
                } catch (Exception e) {
                    logger.info("循环处理提前结清的还款计划数据 ，发生异常，第" + i + "条，借据号：" + loanList.get(i).getRefNbr());
                    // 记录到异常表，后面补上
                    logger.error("删除数据发生异常", e);
                    e.printStackTrace();
                    String tableName = "LoanRepayPlan还款计划表";
                    String erroMsg = "";
                    String tempTotal = "";
                    String acountTotal = "";
                    String type = "0";
                    String updateDelInster = "删除";
                    String no = loanList.get(i).getRefNbr();
                    String eMsg = e.getMessage();
                    accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type, updateDelInster, no, eMsg);

                }
                logger.info("结束 循环处理提前结清的还款计划数据，第" + i + "条，借据号：" + loanList.get(i).getRefNbr());
            }
            logger.info("循环结束");
        }
        return result;
    }

    /**
     * 计算bizdate-1天的值
     * @param dateStr10
     * @return
     */
    private final String getBizDate(String dateStr10) {
        if (StringUtils.isEmpty(dateStr10)) {
            return "";
        }
        return LocalDate.parse(dateStr10, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(-1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

}
